import 'package:dio_log/http_log_list_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:flutterapp/Base/DemoUI/DemoPageGetRouteArgs.dart';
import 'package:flutterapp/Base/DemoUI/DemoPageMain.dart';
import 'package:flutterapp/Base/DemoUI/DemoPageRefreshLoad.dart';
import 'package:flutterapp/Base/DemoUI/DemoPageTab.dart';
import 'package:flutterapp/Base/InitConfig.dart';
import 'package:flutterapp/Base/PageSocketTest.dart';
import 'package:flutterapp/Base/methodChannel/MethodChannelManager.dart';
import 'package:flutterapp/Base/net/Api.dart';
import 'package:flutterapp/Base/net/HttpManager.dart';
import 'package:flutterapp/Base/redux/Actions.dart';
import 'package:flutterapp/Base/redux/reduxConfig.dart';
import 'package:flutterapp/Base/style/MyStyles.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'UIComponent/LoadingConfig.dart';

class PageTest extends StatefulWidget {
  @override
  _PageTestState createState() => _PageTestState();
}

class _PageTestState extends State<PageTest> {
 static const WEB_VIEW_URL_KEY="WEB_VIEW_URL_KEY";
  TextEditingController controller = new TextEditingController();

  @override
  void initState() {
    super.initState();
    controller.text = InitConfig.sPrefs.get(WEB_VIEW_URL_KEY);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("PageTest"),
      ),
      body: Container(
        child: ListView(
          children: <Widget>[
            Row(children: [
              Expanded(child: TextField(controller:controller,),),
              FlatButton(onPressed: (){
                InitConfig.sPrefs.setString(WEB_VIEW_URL_KEY, controller.text);
                myMethodChannelManager.invoke(MethodTypes.OPEN_AGENT_WEB,
                    controller.text);
              }, child: Text("open"))
            ],),
            InkWell(
              child: SizedBox(
                height: 80,
                child: Center(
                  child: Text("show toast"),
                ),
              ),
              onTap: () {
                Fluttertoast.showToast(
                  msg: "show toast",
                );
              },
            ),
            StoreBuilder<AppState>(builder: (context, store) {
              return InkWell(
                child: SizedBox(
                  height: 80,
                  child: Center(
                    child: Text(
                        "(redux total：${store.state.stateNumCountTest
                            .totalCount})  sp count +1"),
                  ),
                ),
                onTap: () async {
//                SharedPreferences.setMockInitialValues({}); // set initial values here if desired
                  SharedPreferences prefs = InitConfig.sPrefs;
                  int counter = (prefs.getInt('counter') ?? 0) + 1;
                  Fluttertoast.showToast(
                    msg: 'Pressed $counter times.',
                  );
                  await prefs.setInt('counter', counter);
                  store.dispatch(ActionNumIncrease(1));
                },
              );
            }),
            InkWell(
              child: SizedBox(
                height: 80,
                child: Center(
                  child: Text(
                    "post",
                    style: MyTextStyle.GREY6_12.copyWith(),
                  ),
                ),
              ),
              onTap: () async {
                myHttp.postForm(Api.testUrl, data: {
                  "username": "年轻人不讲武德",
                  "password": "bigENoneFlash",
                  "repassword": "bigENoneFlash",
                }).then((res) {
                  print(res);
                });
//                http
//                    .get("https://api.sockboom.cc/client/traffic",params: {"token":""})
//                    .then((_) {});
              },
            ),
            InkWell(
              child: SizedBox(
                height: 80,
                child: Center(
                  child: Text("http log"),
                ),
              ),
              onTap: () async {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => HttpLogListWidget(),
                  ),
                );
              },
            ),
            InkWell(
              child: SizedBox(
                height: 80,
                child: Center(
                  child: Text("show google icons"),
                ),
              ),
              onTap: () async {
                // Navigator.push(context, MaterialPageRoute(builder: (context) {
                //   return WebviewScaffold(
                //     url:
                //         "https://material.io/resources/icons/?icon=where_to_vote&style=baseline",
                //   );
                // }));
                myMethodChannelManager.invoke(MethodTypes.OPEN_AGENT_WEB,
                    "https://material.io/resources/icons/?icon=where_to_vote&style=baseline");
              },
            ),
            InkWell(
              child: SizedBox(
                height: 80,
                child: Center(
                  child: Text("test method channel"),
                ),
              ),
              onTap: () async {
                var resp = await myMethodChannelManager.invoke(
                    MethodTypes.INVOKE_DEMO, {"KEY1": 1, "KEY2": "123132"});
                EasyLoading.showToast("$resp");
              },
            ),
            InkWell(
              child: SizedBox(
                height: 80,
                child: Center(
                  child: Text("loading show1 dis2"),
                ),
              ),
              onTap: () async {
                MyLoading.show();
                Future.delayed(Duration(seconds: 2)).then((_) {
                  MyLoading.dismiss();
                  MyLoading.dismiss();
                });
              },
            ),
            InkWell(
              child: SizedBox(
                height: 80,
                child: Center(
                  child: Text("show PAGE MAIN bottom bar"),
                ),
              ),
              onTap: () async {
                Navigator.pushNamed(context, DemoPageMain.ROUTER_NAME);
              },
            ),
            InkWell(
              child: SizedBox(
                height: 80,
                child: Center(
                  child: Text("show PAGE tab"),
                ),
              ),
              onTap: () async {
                Navigator.pushNamed(context, DemoPageTab.ROUTER_NAME);
              },
            ),
            InkWell(
              child: SizedBox(
                height: 80,
                child: Center(
                  child: Text("show err route page"),
                ),
              ),
              onTap: () async {
                Navigator.pushNamed(
                  context,
                  "wuhuqifei",
                );
              },
            ),
            InkWell(
              child: SizedBox(
                height: 80,
                child: Center(
                  child: Text("show err route page"),
                ),
              ),
              onTap: () async {
                Navigator.pushNamed(context, "wuhuqifei");
              },
            ),
            InkWell(
              child: SizedBox(
                height: 80,
                child: Center(
                  child: Text("send msg by route"),
                ),
              ),
              onTap: () async {
                Navigator.pushNamed(context, DemoPageGetRouteArgs.ROUTER_NAME,
                    arguments: "年轻人不讲武德 大e了 没有闪");
              },
            ),
            InkWell(
              child: SizedBox(
                height: 80,
                child: Center(
                  child: Text("refresh & loadMore"),
                ),
              ),
              onTap: () async {
                Navigator.pushNamed(
                  context,
                  DemoPageRefreshLoad.ROUTER_NAME,
                );
              },
            ),
            InkWell(
              child: SizedBox(
                height: 80,
                child: Center(
                  child: Text("manual check update"),
                ),
              ),
              onTap: () async {
                myMethodChannelManager.invoke(MethodTypes.CHECK_UPDATE);
              },
            ),
            InkWell(
              child: SizedBox(
                height: 80,
                child: Center(
                  child: Text("test err"),
                ),
              ),
              onTap: () async {
                // throw 'bugly flutter uncaught error test';
                List list = [];
                print(list[8]);
              },
            ),
            InkWell(
              child: SizedBox(
                height: 80,
                child: Center(
                  child: Text("test socket"),
                ),
              ),
              onTap: () async {
                Navigator.pushNamed(context, PageSocketTest.ROUTER_NAME);
              },
            ),

          ],
        ),
      ),
    );
  }

}

